package Thread;

import javax.sound.sampled.FloatControl;

//线程安全：使用加锁解决线程安全
class Counter{
    Object locker = new Object();
    public int count = 0;
    public void increase(){
        synchronized (locker){
            count++;
        }
    }
    public void increase2(){
        synchronized (locker){
            count++;
        }
    }
    public static void func(){
        synchronized (Counter.class){

        }
    }

}
public class Demo12 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                counter.increase2();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                counter.increase();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.count);
    }
}
